using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using CrazyDogToDo.Service.Services;
using Microsoft.AspNetCore.Http;
using CrazyDogToDo.Entity.Data;
using CrazyToDo.Web.Models;

namespace CrazyToDo.Web.Controllers
{
    public class InventoryController : Controller
    {
        private readonly MatterService _matterService = new MatterService();
        private readonly InventoryService _InventoryService = new InventoryService();
        public IActionResult Index()
        {
            List<Matter> matters = _matterService.GetAllMatter(HttpContext.Session.GetString("user")).ToList();
            List<Inventory> inventory = _InventoryService.GetAllInventory(HttpContext.Session.GetString("user")).ToList();
            List<InventoryViewModel> inventoryViewModels = inventory.Select(item => new InventoryViewModel
            {
                Id = item.Id,
                Email = item.Email,
                Title = item.Title,
                CreateTime = item.CreateTime,
            }).ToList();
            return View("Inventory", inventoryViewModels);
        }

        [HttpGet]
        public IActionResult GetAllInventory()
        {
            List<Inventory> inventory = _InventoryService.GetAllInventory(HttpContext.Session.GetString("user")).ToList();
            List<InventoryViewModel> inventoryViewModels = inventory.Select(item => new InventoryViewModel
            {
                Id = item.Id,
                Email = item.Email,
                Title = item.Title,
                CreateTime = item.CreateTime
            }).ToList();
            return View("Inventory", inventoryViewModels);
        }
        [HttpGet]
        public IActionResult GetAllListMatter(int id)
        {
            List<Matter> matters = _matterService.GetListMatter(id).ToList();
            if(matters.Count == 0)
            {
                List<Inventory> inventory = _InventoryService.GetInventoryById(HttpContext.Session.GetString("user"), id);
                List<InventoryViewModel> inventoryViewModels = inventory.Select(item => new InventoryViewModel
                {
                    Id = item.Id,
                    Email = item.Email,
                    Title = item.Title,
                    CreateTime = item.CreateTime
                }).ToList();
                return View("GetNullIncentoryMatter", inventoryViewModels);
                
            }
            List<MatterViewModel> matterViewModels = matters.Select(item => new MatterViewModel
            {
                Id = item.Id,
                InventoryId = item.InventoryId,
                Content = item.Content,
                Remark = item.Remark,
                CreateTime = item.CreateTime,
                OverdueTime = item.OverdueTime,
                State = item.State
            }).ToList();
            return View("GetAllListMatter", matterViewModels);


        }

        public IActionResult DeleteMatterInInventory(int listId, int id)
        {
            _matterService.DeleteMatter(id);
            List<Matter> matters = _matterService.GetListMatter(listId).ToList();

            if (matters.Count == 0)
            {
                List<Inventory> inventory = _InventoryService.GetInventoryById(HttpContext.Session.GetString("user"), listId);
                List<InventoryViewModel> inventoryViewModels = inventory.Select(item => new InventoryViewModel
                {
                    Id = item.Id,
                    Email = item.Email,
                    Title = item.Title,
                    CreateTime = item.CreateTime
                }).ToList();
                return View("GetNullIncentoryMatter", inventoryViewModels);
            }
            else
            {
                List<MatterViewModel> matterViewModels = matters.Select(item => new MatterViewModel
                {
                    Id = item.Id,
                    InventoryId = item.InventoryId,
                    Content = item.Content,
                    Remark = item.Remark,
                    CreateTime = item.CreateTime,
                    OverdueTime = item.OverdueTime,
                    State = item.State
                }).ToList();
                return View("MymatterInventory", matterViewModels);
            }
            
        }


        public IActionResult DeleteInventory(int id)
        {
            _InventoryService.DeleteInventory(id);
            List<Inventory> inventory = _InventoryService.GetAllInventory(HttpContext.Session.GetString("user")).ToList();
            List<InventoryViewModel> inventoryViewModels = inventory.Select(item => new InventoryViewModel
            {
                Id = item.Id,
                Email = item.Email,
                Title = item.Title,
                CreateTime = item.CreateTime
            }).ToList();
            return View("MyInventory", inventoryViewModels);
        }

        public IActionResult AddInventory(string title)
        {
            _InventoryService.AddInventory(HttpContext.Session.GetString("user"), title);
            return RedirectToAction("Index");
        }

        public IActionResult AddMatterToInventory(string content, string remark, string overdueTime,int listId)
        {
            _matterService.AddMatterToInventory(HttpContext.Session.GetString("user"), content, remark, overdueTime, listId);
            return RedirectToAction("Index");
        }

        public IActionResult ChangeState(int id,int listId)
        {
            _matterService.ChangeMatterState(HttpContext.Session.GetString("user"), id);
            var matters = _matterService.GetListMatter(listId);
            List<MatterViewModel> matterViewModels = matters.Select(item => new MatterViewModel
            {
                Id = item.Id,
                InventoryId = item.InventoryId,
                State = item.State,
                CreateTime = item.CreateTime,
                Content = item.Content,
                OverdueTime = item.OverdueTime,
                Email = item.Email,
                Remark = item.Remark
            }).ToList();
            return View("ChangeStatus", matterViewModels);
        }
    }
}